package csl.mq.rabbitmq.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description: TODO 初始化工具
 */
public class RabbitMQUtils {

    private static final ConnectionFactory factory = new ConnectionFactory();
    /**
     * 创建连接工厂并初始化连接
     * @return
     */
    public static Connection initRabbitMQ() throws IOException, TimeoutException {
        factory.setHost("106.14.246.151");
        factory.setPort(5672);
        factory.setUsername("policymaker");
        factory.setPassword("123");
        factory.setVirtualHost("/csl");
        return factory.newConnection();
    }

    public static void closeRabbitMQ(Connection connection, Channel channel) throws IOException, TimeoutException {
        if (null!=channel){
            channel.close();
        }
        if (connection!=null){
            connection.close();
        }
    }
}
